package work.timejay.user.service.books.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import work.timejay.bo.CopyCategoryItemBO;
import work.timejay.common.ApiPageEntity;
import work.timejay.common.SuperPage;
import work.timejay.enums.BooksCatalogueEnums;
import work.timejay.exception.TimeJayException;
import work.timejay.user.dto.books.BooksCatalogueInfoDTO;
import work.timejay.user.dto.books.BooksCataloguePageDTO;
import work.timejay.user.entity.books.Books;
import work.timejay.user.vo.books.BooksCataloguePageVO;
import work.timejay.user.entity.books.BooksCatalogue;
import work.timejay.user.mapper.books.BooksCatalogueMapper;
import work.timejay.user.service.books.BooksCatalogueService;
import work.timejay.common.SuperServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 目录表 服务实现类
 * </p>
 *
 * @author 王文杰
 * @since 2023-12-11
 */
@Service
public class BooksCatalogueServiceImpl extends SuperServiceImpl<BooksCatalogueMapper, BooksCatalogue> implements BooksCatalogueService {

    @Override
    public void saveBatchInfo(List<CopyCategoryItemBO> itemBOList, Long bookId) {
        List<BooksCatalogue> voList = BeanUtil.copyToList(itemBOList, BooksCatalogue.class);
        voList.forEach(e -> {
            if(StringUtils.isBlank(e.getTitle())){
                e.setTitle("未知章节");
            }
            if(StringUtils.isBlank(e.getSourceLink())){
                e.setTitle("未知链接");
            }
            e.setId(IdWorker.getId());
            e.setBookId(bookId);
        });


        saveBatch(voList);
    }

    @Override
    public void isExistsToDeleteByBookId(Long bookId) {
        QueryWrapper<BooksCatalogue> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(BooksCatalogue::getBookId, bookId);
        baseMapper.delete(qw);
    }

    @Override
    public ApiPageEntity<BooksCataloguePageVO> pageInfo(BooksCataloguePageDTO<BooksCatalogue> dto) {
        Page<BooksCatalogue> page = lambdaQuery()
                .eq(BooksCatalogue::getBookId, dto.getBookId())
                .orderByAsc(BooksCatalogue::getOrderNum)
                .page(dto.getPage());
        if (null == page || CollectionUtil.isEmpty(page.getRecords())) {
            return ApiPageEntity.createByEmpty(new ArrayList<>());
        }
        List<BooksCataloguePageVO> voList = BeanUtil.copyToList(page.getRecords(), BooksCataloguePageVO.class);
        return ApiPageEntity.create(page, voList);
    }

    @Override
    public BooksCatalogue getInfo(BooksCatalogueInfoDTO dto) {
        QueryWrapper<BooksCatalogue> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(BooksCatalogue::getId, dto.getId());
        Long orderNum = dto.getOrderNum();
        if (BooksCatalogueEnums.type.CURRENT.getType() == dto.getType()) {
            return baseMapper.selectOne(qw);
        }
        if (BooksCatalogueEnums.type.BEFORE.getType() == dto.getType()) {
            if (null == orderNum) {
                throw TimeJayException.fail("500", "orderNum不能为空");
            }
            orderNum--;
            qw.lambda()
                    .eq(BooksCatalogue::getOrderNum, orderNum);
            return baseMapper.selectOne(qw);
        }
        if (BooksCatalogueEnums.type.AFTER.getType() == dto.getType()) {
            if (null == orderNum) {
                throw TimeJayException.fail("500", "orderNum不能为空");
            }
            orderNum++;
            qw.lambda()
                    .eq(BooksCatalogue::getOrderNum, orderNum);
            return baseMapper.selectOne(qw);
        }
        throw TimeJayException.fail("500", "请求类型异常");
    }

    @Override
    public List<BooksCatalogue> findBooksCatalogueInfoList() {
        return lambdaQuery()
                .eq(BooksCatalogue::getHandlerStatus,0)
                .isNotNull(BooksCatalogue::getSourceLink)
                .last("LIMIT 1000000")
                .list();
    }
}
